import {
  request
} from '../../api/index'
import {
  fillAll
} from '../../utils/util'
import {
  DUSTING,
  hasErr,
  inputValidHandle
} from '../../utils/valid'

const app = getApp()

const form = {
  powderParticleGrading: {
    name: 'PW1',
    type: '粉料颗粒级配',
    value: '',
    err: ''
  },
  powderBulkDensity: {
    name: 'PW2',
    type: '粉料容重',
    value: '',
    err: '',
    desc: DUSTING['powderBulkDensity'].unit
  },
  powderMoisture: {
    name: 'PW3',
    type: '粉料水分',
    value: '',
    err: '',
    desc: DUSTING['powderMoisture'].unit
  },
  shrinkage: {
    name: 'PW4',
    type: '收缩率',
    value: '',
    err: '',
    desc: DUSTING['shrinkage'].unit
  },
  waterAbsorption: {
    name: 'PW5',
    type: '吸水率',
    value: '',
    err: '',
    desc: DUSTING['waterAbsorption'].unit
  },
  whiteness: {
    name: 'PW6',
    type: '白度',
    value: '',
    err: '',
    desc: DUSTING['whiteness'].unit
  },
  lossOnIgnition: {
    name: 'PW7',
    type: '烧失量',
    value: '',
    err: '',
    desc: DUSTING['lossOnIgnition'].unit
  }
}
Page({

  /**
   * 页面的初始数据
   */
  data: {
    
    form,
    fields: Object.keys(form),
    canNext: false,
    id: '',
    show: false,
    levels: '{}',
    fromNavigation: 0,
    recordState: app.globalData.editState
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    this.setPageProperty(options);
    this.fetchData();
  },

  /**
   * 设置页面属性
   */
  setPageProperty(options) {
    wx.setNavigationBarTitle({
      title: '2-坯体喷粉'
    })
    this.setData({
      id: options.id,
      fromNavigation: options.navigation || 0,
      recordState: app.globalData.editState
    })
  },
  fetchData() {
    request.dustingList({
      collectionRecordIds: this.data.id,
    }, (e) => {
      let k = Object.assign({}, this.data.form);
      e[0] && this.data.fields.forEach(item => {
        k[item].value = e[0][item],
          k.id = e[0].id
      })
      this.setData({
        form: k,
        levels: e[0]?.powderParticleGrading
      })
    })
  },
  onInput(event) {
    const key = event.currentTarget.dataset.key,
      value = event.detail,
      form = this.data.form;
    const target = DUSTING[key];
    if (inputValidHandle(value, DUSTING, key) === false) {
      form[key]['err'] = '数值不正确：' + target.desc;
    } else {
      form[key]['err'] = '';
    }
    form[key]['value'] = String(value);
    this.setData({
      form
    })
  },
  nextHandle() {
    if (this.data.fromNavigation > 0) {
      wx.navigateBack({
        delta: 1
      })
      return
    }
    wx.redirectTo({
      url: '../molding/index?id=' + this.data.id
    })
  },
  toHome() {
    wx.navigateBack({
      delta: 4
    })
  },
  onChange(event) {
    if (hasErr(this.data.form)) {
      wx.showToast({
        title: '存在不合理的数值',
        icon: 'none'
      })
      return
    }
    fillAll(event.detail.value, true) && request.dustingAdd(Object.assign({
      collectionRecordId: this.data.id
    }, this.data.formResult, event.detail.value, {
      id: this.data.form.id
    }, {
      powderParticleGrading: this.data.form.powderParticleGrading.value
    }), this.nextHandle)
  },
  onFocus(event) {
    if (event.currentTarget.dataset.name === 'powderParticleGrading') {
      this.setData({
        show: true
      })
    }
  },
  dustCloseHandle() {
    this.setData({
      show: false
    })
  },
  dustingSubmitHandle(event) {
    this.setData({
      form: {
        ...this.data.form,
        powderParticleGrading: {
          name: 'PW1',
          type: '粉料颗粒级配',
          value: JSON.stringify(event.detail.value)
        },
      }
    })
    this.dustCloseHandle();
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    wx.onAppRoute((res) => {
      if (res.path === 'pages/dusting/index' && res.openType === 'navigateBack') {
        this.fetchData();
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})